package cn.wolfcode.luowowo2.search.service.impl;

import cn.wolfcode.luowowo2.search.query.SearchQueryObject;
import cn.wolfcode.luowowo2.search.repository.ITravelTemplateRepository;
import cn.wolfcode.luowowo2.search.service.ITravelSearchService;
import cn.wolfcode.luowowo2.search.template.TravelTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

/**
 * Created by Albert on 2019/7/15.
 */
@Service
public class TravelSearchServiceImpl implements ITravelSearchService{
    @Autowired
    private ITravelTemplateRepository dao;

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private TransportClient client;

    @Override
    public void createIndex() {
        template.createIndex(TravelTemplate.class);
        template.putMapping(TravelTemplate.class);
        
    }

    @Override
    public void save(TravelTemplate template) {
        dao.save(template);
    }

    @Override
    public Page<TravelTemplate> query(SearchQueryObject qo) {
        //Iterable<TravelTemplate> travelTemplates = dao.search(QueryBuilders.termQuery("destId", qo.getDestId()));
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (qo.getDestId() != -1) {
            boolQuery.must(QueryBuilders.termQuery("destId",qo.getDestId()));
        }
        Page<TravelTemplate> result = dao.search(boolQuery, qo.getPageableWithOutSort());
        return result;
    }
}
